import { getShopList, delShopList, AddShopList } from '@/api/shop.js'
export default {
	state: {
		list: [],
		selectedList: [],
		checkedAll: false
	},
	getters: {
		list: state => {
			return state.list || []
		},
		checkedAll: state => {
			return state.selectedList.length === state.list.length
		},
		totalCount: state => {
			let total = {
				price: 0,
				num: 0
			}
			total.num = state.selectedList.length
			state.list.forEach(n => {
				if (state.selectedList.indexOf(n.id) > -1) {
					total.price += n.unitPrice*n.buyCount
				}
			})
			return total
		}
	},
	mutations: {
		setList (state, list) {
			state.list = list || []
		},
		addSelectList(state, data) {
			state.list.push(data)
		},
		// 全选
		selectAllList (state) {
			state.selectedList = state.list.map(n => {
				n.checked = true
				return n.id
			})
		},
		// 全不选
		setSelectedNoAllList (state, checkedAll) {
			state.list.forEach(n => {
				n.checked = false
			})
			state.selectedList = []
		},
		selectItem(state, index) {
			try{
				let id = state.list[index].id
				let i = state.selectedList.indexOf(id)
				// 如果id在已选中的selectedList 里面，删除该id项，并且当前项list中的checked 改为false
				if (i > -1) {
					state.list[index].checked = false
					return state.selectedList.splice(i, 1)
				}
				// 如果之前没有选中，在selectedList加入id，并且把该项list中的checked 改为 true
				state.list[index].checked = true
				state.selectedList.push(id)
			} catch (e) {
				console.error(e)
			}
		},
		setDelGoods (state) {
			state.list = state.list.filter(n => {
				return state.selectedList.indexOf(n.id) === -1
			})
			uni.showToast({
				title:'删除成功',
				icon: 'none'
			})
		}
	},
	actions: {
		handleTapAll({commit, getters}) {
			return new Promise((resolve, reject) => {
				if (getters.checkedAll) {
					commit('setSelectedNoAllList')
				} else {
					commit('selectAllList')
				}
				resolve()
			}).catch(error => {
				reject(error)
			})
		},
		delGoods({commit, state}) {
			return new Promise((resolve, reject) => {
				let data = {
					shoppingCartId: state.selectedList.join(',')
				}
				delShopList(data).then(res => {
					if (res.data.code === '200' || res.data.code === 200) {
						commit('setDelGoods')
						commit('setSelectedNoAllList')
					} else {
						return
					}
				})
				resolve()
			}).catch(error => {
				reject(error)
			})
		},
		addGoods({commit, state}, data) {
			return new Promise((resolve, reject) => {
				let isClude = state.list.find(n => n.id === data.id)
				if (isClude) {
					uni.showToast({title: '商品已经存在购物车，请勿重复提交！',icon: 'none'});
					return
				}
				AddShopList(data).then(res => {
					if (res.data.code === '200' || res.data.code === 200) {
						uni.showToast({title: '增加购物车成功',icon: 'none'});
					}
				})
				resolve()
			}).catch(error => {
				reject(error)
			})
		},
		getShopList({commit}, data) {
			return new Promise((resolve, reject) => {
				getShopList(data).then(res => {
					let dataList = (res.data.data || [])
					commit('setList', dataList)
					resolve(dataList)
				})
			}).catch(error => {
				reject(error)
			})
		}
	}
}